package com.example.bookstore;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebBackForwardList;
import android.widget.LinearLayout;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class selectResult extends AppCompatActivity {
    private String selectData;
    private List<Book> bookList = new LinkedList<>();
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_result);
        Intent intent = getIntent();
        selectData = intent.getStringExtra("selectData");
        init();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView = (RecyclerView)findViewById(R.id.select_result);
        recyclerView.setLayoutManager(layoutManager);

    }

    private void init() {
       getUrl(selectData);

    }


    private void getUrl(String category){
        new Thread(()->{
            try {
                OkHttpClient client=new OkHttpClient();
                RequestBody requestBody=new FormBody.Builder()
                        .add("bookName",category)
                        .build();
                Request request=new Request.Builder()
                        .url("http://www.fenchingking.top/BookStore/getBookByName")
                        .post(requestBody)
                        .build();
                Response response=client.newCall(request).execute();
                String responseData=response.body().string();
                change(responseData);
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }
    private void change(final String str){
        runOnUiThread(() ->{
                    try {
                        JSONArray jsonArray=new JSONArray(str);
//                        Toast.makeText(this,jsonArray.get(0).toString(),Toast.LENGTH_SHORT).show();
                        for(int i = 0;i<jsonArray.length();++i){
                            JSONObject jsonObject =  jsonArray.getJSONObject(i);
                            bookList.add(getBook(jsonObject));
                        }
                        recyclerView.setAdapter(new BooksAdapter(bookList));
//                        Toast.makeText(this,""+bookList.size(),Toast.LENGTH_SHORT).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }


        );
    }

    private Book getBook(JSONObject jsonObject) throws JSONException {
        Book book = new Book();
        book.setBookName(jsonObject.get("bookName").toString());
        book.setBookPrice(jsonObject.getString("bookPrice"));
        book.setBookRest(jsonObject.getInt("bookRest"));
        book.setBookTitle(jsonObject.getString("bookTitle"));
        book.setBookImage(jsonObject.get("bookImage").toString());
        book.setBookDescribe(jsonObject.getString("bookDescribe"));
        book.setPressDate(jsonObject.getString("pressDate"));
        book.setBookAuthor(jsonObject.getString("bookAuthor"));
        book.setBookCategory(jsonObject.getString("bookCategory"));
        book.setId(jsonObject.getInt("bookId"));
        return book;
    }
}
